﻿using System.Collections.Generic;
using UnityEngine;

namespace TC
{
    public class EventEntity
    {
        public int eventType;
        public Dictionary<string, Variable> paramsDic;
    }

    /// <summary>
    /// 触发Event
    /// </summary>
    public class TriggerEventNode : FunctionNode
    {
        public EventEntity entity;
        
        public TriggerEventNode(EventEntity entity)
        {
            this.entity = entity;
        }

        public override void OnCreate()
        {
            base.OnCreate();

            foreach (var i in entity.paramsDic)
            {
                if (i.Value is ByteVar)
                {
                    this.AddValueInPort<byte>(i.Key);
                }
                else if (i.Value is ShortVar)
                {
                    this.AddValueInPort<short>(i.Key);
                }
                else if (i.Value is IntVar)
                {
                    this.AddValueInPort<int>(i.Key);
                }
                else if (i.Value is LongVar)
                {
                    this.AddValueInPort<long>(i.Key);
                }
                else if (i.Value is FloatVar)
                {
                    this.AddValueInPort<float>(i.Key);
                }
                else if (i.Value is DoubleVar)
                {
                    this.AddValueInPort<double>(i.Key);
                }
                else if (i.Value is CharVar)
                {
                    this.AddValueInPort<char>(i.Key);
                }
                else if (i.Value is BoolVar)
                {
                    this.AddValueInPort<bool>(i.Key);
                }
                else if (i.Value is VectorVar)
                {
                    this.AddValueInPort<Vector4>(i.Key);
                }
                else if (i.Value is ObjectVar)
                {
                    this.AddValueInPort<object>(i.Key);
                }
            }
        }

        #region flow
        public override void Execute(Dispatcher chain)
        {
            base.Execute(chain);
            
            List<ValuePort> ls = this.GetPorts<ValuePort>();
            Context e = new Context();
            foreach (var i in ls)
            {
                if (!i.IsInput) continue;

                if (i.type == typeof(byte))
                {
                    ValueInput<byte> vi = i as ValueInput<byte>;
                    e.AddVariable(i.name, vi.Value);
                }
                else if (i.type == typeof(short))
                {
                    ValueInput<short> vi = i as ValueInput<short>;
                    e.AddVariable(i.name, vi.Value);
                }
                else if (i.type == typeof(int))
                {
                    ValueInput<int> vi = i as ValueInput<int>;
                    e.AddVariable(i.name, vi.Value);
                }
                else if (i.type == typeof(long))
                {
                    ValueInput<long> vi = i as ValueInput<long>;
                    e.AddVariable(i.name, vi.Value);
                }
                else if (i.type == typeof(float))
                {
                    ValueInput<float> vi = i as ValueInput<float>;
                    e.AddVariable(i.name, vi.Value);
                }
                else if (i.type == typeof(double))
                {
                    ValueInput<double> vi = i as ValueInput<double>;
                    e.AddVariable(i.name, vi.Value);
                }
                else if (i.type == typeof(char))
                {
                    ValueInput<char> vi = i as ValueInput<char>;
                    e.AddVariable(i.name, vi.Value);
                }
                else if (i.type == typeof(bool))
                {
                    ValueInput<bool> vi = i as ValueInput<bool>;
                    e.AddVariable(i.name, vi.Value);
                }
                else if (i.type == typeof(Vector2))
                {
                    ValueInput<Vector4> vi = i as ValueInput<Vector4>;
                    e.AddVariable(i.name, vi.Value);
                }
                else if (i.type == typeof(Vector3))
                {
                    ValueInput<Vector4> vi = i as ValueInput<Vector4>;
                    e.AddVariable(i.name, vi.Value);
                }
                else if (i.type == typeof(Vector4))
                {
                    ValueInput<Vector4> vi = i as ValueInput<Vector4>;
                    e.AddVariable(i.name, vi.Value);
                }
                else if (i.type == typeof(object))
                {
                    ValueInput<object> vi = i as ValueInput<object>;
                    e.AddVariable(i.name, vi.Value);
                }
            }

            //发送事件
            EventCenter.Instance.SendEvent<Context>(entity.eventType, e);
        }
        #endregion
    }
}
